version 10.1
#delimit;
clear;
pause on;
set more off;
  quietly log;
  local logon = r(status);
  if "`logon'" == "on" {; log close; };
log using table2, text replace;

/************************************************************************/
/* 	Author:		Olga Chyzh and Frederick J. Boehmke		*/
/*	Date:		May 14, 2015			   	  	*/
/*      File:		table2.do				  	*/
/*	Purpose:	Replicate Long (2008) to use as an example of 	*/
/*			S-scores.					*/
/*      Input File:	long2008replication.dta				*/
/*      Output File:	table2.log,					*/
/*			table3.txt,					*/
/*			table8.txt					*/
/*	Requires:	scompute.ado (Stata package from Signorino).	*/
/************************************************************************/

	
clear;	
clear matrix;
clear mata;
set mem 500m;
set seed 10;	
	
	/*****************************************************************/
	/* This program combines the results from the imputed data sets. */
	/*****************************************************************/

capture program drop midisp;
program define midisp;

	local names: colnames(Q1);

	matrix Q = (Q1 + Q2 + Q3 + Q4 + Q5)/5;		/* MI param estimates */
	matrix W = (W1 + W2 + W3 + W4 + W5)/5;		/* MI covariances     */
	local  k = colsof(Q);

	forvalues i=1/5 {;

	  matrix QQ=Q`i'-Q;

	  if `i'==1 {;

		matrix B=(QQ)'*QQ;

		};

	  else matrix B = B + (QQ)'*QQ;


	};

	matrix B=B/(5-1);				/* Covariance adjustment */

	matrix V=W+(1+1/5)*B;				/* Final covariance   */

	ereturn post Q V;
	ereturn display, plus neq(1);
	

end;

tempfile data temp_s;
	
/*Opening the main data*/
use long2008replication, clear;

/*Run a model that predicts Alliance Type (Table 8)*/
reg ally_type reg_sim major1 major2 lprsecf1 lprsecf2 ldist strival tot_ally1 tot_ally2;

  matrix B = e(b);
  matrix C = e(V);

estimates store allies;

estout allies using table8.txt, style(tab) replace 
	cells("b(fmt(3) star) se(par)")
	modelwidth(8)
	starlevels(* 0.05 ** 0.01 *** 0.001) legend
	varwidth(30)
	order(reg_sim major1 major2 lprsecf1 lprsecf2 ldist strival tot_ally1 tot_ally2)
	collabels(, none)
	varlabels(_cons "Constant"
				reg_sim "Regime Similarity"
				major1 "Major Power (i)"
				major1 "Major Power (j)"
				lprsecf1  "External Conflict Risk (it)"
				lprsecf2  "External Conflict Risk (jt)"
				ldist "Distance (ijt)"
				strival "Strategic Rivalry (ij)"
				tot_ally1 "Total Number of Allies (i)"
				tot_ally2 "Total Number of Allies (j)");

predict allies_hat;

/*Generate the polynomials to predict s-scores (Keleijian approach)*/
foreach var of varlist lprsecf1 lprsecf2 ldist tot_ally1 tot_ally2 {;
gen `var'_sq=`var'^2;
gen `var'_cube=`var'^3;
};

reg lns reg_sim major1 major2 lprsecf1 lprsecf2 ldist strival tot_ally1 tot_ally2
lprsecf1_sq lprsecf2_sq ldist_sq tot_ally1_sq tot_ally2_sq
lprsecf1_cube lprsecf2_cube ldist_cube tot_ally1_cube tot_ally2_cube;
predict lns_instr;



save `data', replace;

/*Create i-i dyads*/
/*Since the version of s-scores used in Long (2008) is weighted by capabilities,*/
/* the following commands create the capabilities variables for both states in the dyad */
collapse cap_1, by(ccode1 year) cw;
gen ccode2=ccode1;
gen cap_2= cap_1;

append using `data';
recode allies_hat ally_type (.=3)  if ccode1==ccode2;
  drawnorm b1 b2 b3 b4 b5 b6 b7 b8 b9 b0, mean(B) cov(C);

  /*Note that the -scompute- program used below does not work well with locals. Hence, we save a copy of the data file before computing s-scores.*/
save data.dta, replace;

/*Compute s-scores on the predicted alliance type variable (to use in the S-score of IV model).  Do this five times.*/
forvalues i=1/5 {;

  generat allies_hat`i' = b0[`i'] + b1[`i']*reg_sim + b2[`i']*major1 + b3[`i']*major2 + b4[`i']*lprsecf1 + b5[`i']*lprsecf2 
	+ b6[`i']*ldist + b7[`i']*strival + b8[`i']*tot_ally1 + b9[`i']*tot_ally2;


		/*Need to save data for scompute to run*/
	
	save temp.dta, replace;
	scompute ccode1 ccode2, id(year) weightvar(cap_2) svar(allies_hat`i');
	save temp_s.dta, replace; /*using a local instead seems to lead to crashing before the next scompute command*/

	use temp.dta, clear;
	scompute ccode2 ccode1, id(year) weightvar(cap_1) svar(allies_hat`i');
	append using temp_s.dta;

	merge 1:1 ccode1 ccode2 year using data.dta, gen(_merge_s`i'_hat);
	drop if _merge_s`i'_hat==1;
	save data.dta, replace;

  };
	

use temp.dta, clear;
scompute ccode1 ccode2, id(year) weightvar(cap_2) svar(ally_type);
save temp_s.dta, replace; 

use temp.dta, clear;
scompute ccode2 ccode1, id(year) weightvar(cap_1) svar(ally_type);
append using temp_s.dta;

merge 1:1 ccode1 ccode2 year using data.dta, gen(_merge_s1);

/*Drop the merge-related unnecessary variables*/
drop if _merge_s1==1;
drop if ccode1==ccode2;
drop _merge*;

/*Log s-scores of IVs to be consistent with Long (2008)*/
gen lns_constr=ln(S_ally_type+1);
gen lns_hat1=ln(S_allies_hat1 + 1);
gen lns_hat2=ln(S_allies_hat2 + 1);
gen lns_hat3=ln(S_allies_hat3 + 1);
gen lns_hat4=ln(S_allies_hat4 + 1);
gen lns_hat5=ln(S_allies_hat5 + 1);

/*Naive Model*/

tsset ddyad year;
xtpcse ldtrad lgdp1 lgdp2 lgdppc1 lgdppc2 ldist border wtopta jdem allies lns doarmcf1 
doarmcf2 moarmcf1 moarmcf2 dyarmcf lprsicf1 lprsicf2 lprsecf1 lprsecf2 strival, c(ar1) hetonly;

  estimates store long2008;	

/*Replicated Model*/

xtpcse ldtrad lgdp1 lgdp2 lgdppc1 lgdppc2 ldist border wtopta jdem allies lns_constr doarmcf1 
doarmcf2 moarmcf1 moarmcf2 dyarmcf lprsicf1 lprsicf2 lprsecf1 lprsecf2 strival, c(ar1) hetonly;

  estimates store repl_long2008;
  
/*IV of S score (ivlogit)*/

ivregress 2sls ldtrad lgdp1 lgdp2 lgdppc1 lgdppc2 ldist border wtopta jdem allies  doarmcf1 
doarmcf2 moarmcf1 moarmcf2 dyarmcf lprsicf1 lprsicf2 lprsecf1 lprsecf2 strival (lns=reg_sim major1 major2 lprsecf1 lprsecf2 ldist strival tot_ally1 tot_ally2
lprsecf1_sq lprsecf2_sq ldist_sq tot_ally1_sq tot_ally2_sq
lprsecf1_cube lprsecf2_cube ldist_cube tot_ally1_cube tot_ally2_cube);
estimates store long_ivsscore;

/*S Score of IV Model*/

nois _dots 0, title(Running model on each imputed data set) reps(5);

  forvalues i=1/5 {;

  	quietly xtpcse ldtrad lgdp1 lgdp2 lgdppc1 lgdppc2 ldist border wtopta jdem allies lns_hat`i' 
	  doarmcf1 doarmcf2 moarmcf1 moarmcf2 dyarmcf lprsicf1 lprsicf2 lprsecf1 lprsecf2 
	  strival, c(ar1) hetonly;

  	matrix Q`i' = e(b);
  	matrix W`i' = e(V);
	local  ll`i' = e(ll);
	
	nois _dots `i' 0;
	
  	};

	midisp;
	
		/* -estout- seems to be  necessary to get it to -estimates store-. */
	
	estout, cells(b se(par));
	estimates store allies_mi;
	
	di "Average log-likelihood: " (`ll1' + `ll2' + `ll3' + `ll4' + `ll5')/5;

  
estout long2008 allies_mi long_ivsscore using table2.txt, style(tab) replace 
	cells(b(fmt(3) star) se(par))
	modelwidth(8)
	starlevels(* 0.05 ** 0.01 *** 0.001) legend
	varwidth(30)
	order(lns_hat5 lns 
	doarmcf1 doarmcf2 moarmcf1 moarmcf2 dyarmcf lprsicf1 lprsicf2 lprsecf1 lprsecf2 
	lgdp1 lgdp2 lgdppc1 lgdppc2 ldist wtopta border  jdem allies 
	 strival)
	collabels(, none)
	varlabels(lns_hat5	"Logged S-Scores (Estimated)"
	          lns		"Logged S-scores (Long 2008)"
			  lns_constr  "Logged S-scores (Our Replication)"
			   doarmcf1   "Domestic Armed Conflict (it)"
			   doarmcf2   "Domestic Armed Conflict (jt)"
			    moarmcf1  "Interstate Armed Conflict (it)"
				moarmcf2  "Interstate Armed Conflict (jt)"
				dyarmcf   "Interstate Armed Conflict (ijt)"
				lprsicf1  "Internal Conflict Risk (it)"
				lprsicf2  "Internal Conflict Risk (jt)"
				lprsecf1  "External Conflict Risk (it)"
				lprsecf2  "External Conflict Risk (jt)"
				lgdp1  "Gross Domestic Product (it)"
				lgdp2  "Gross Domestic Product (jt)"
				lgdppc1 "GDP/capita (it)"
				lgdppc2 "GDP/capita (jt)"
				ldist "Distance (ijt)"
				wtopta "PTA (ijt)"
				border "Border (ijt)"
				jdem "Joint Democracy (ijt)"
				allies "Allies (ijt)");
				
/*Erase unnecessary data files*/
erase temp.dta;
erase data.dta;
erase temp_s.dta;
erase S_allies_hat1_computed.dta;
erase S_allies_hat2_computed.dta;
erase S_allies_hat3_computed.dta;
erase S_allies_hat4_computed.dta;
erase S_allies_hat5_computed.dta;
erase S_ally_type_computed.dta;

log close;
clear;
exit, STATA;
